package com.hz.framework.MyBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 公共查询后分页方法
 * */
public class NewPageBean extends PageQuery implements Serializable{

	int currentPage = 1;// 当前页数

	int totalPages = 0;// 总页数

	int pageRecorders = 10;// 每页显示行数

	int totalRows = 0;// 总行数

	int pageStartRow = 0;// 每页的起始行数

	int pageEndRow;// 每页的终止行数

	public boolean hasNextPage = false;// 是否有下一页

	public boolean hasPreviousPage = false;// 是否有前一页
	
	String SearcHql="";
	
	Iterator it = null;
	
	public NewPageBean(String SearcHql,int pagecount) {
		
		if(pagecount>0){
			pageRecorders = pagecount ;
		}
		this.SearcHql = SearcHql;

		totalRows = getRowCountHql(SearcHql);

		hasPreviousPage = false;

		currentPage = 1;

		if ((totalRows % pageRecorders) == 0) {

			totalPages = totalRows / pageRecorders;

		}

		else {

			totalPages = totalRows / pageRecorders + 1;

		}

		/** 根据当前页数 和总页数 判断 是否有下一页 */
		if (currentPage >= totalPages) {

			hasNextPage = false;

		}
		else {

			hasNextPage = true;

		}
		if (totalRows < pageRecorders) {

			this.pageStartRow = 0;

			this.pageEndRow = totalRows;

		}
		else {

			this.pageStartRow = 0;

			this.pageEndRow = pageRecorders;

		}

	}

	public void setCurrentPage(int currentPage) {

		this.currentPage = currentPage;

	}

	public void setPageRecorders(int pageRecorders) {

		this.pageRecorders = pageRecorders;

	}

	public void setHasNextPage(boolean hasNextPage) {

		this.hasNextPage = hasNextPage;

	}

	public void setHasPreviosPage(boolean hasPreviosPage) {

		this.hasPreviousPage = hasPreviosPage;

	}

	public String getCurrentPage() {

		return this.toString(currentPage);

	}

	public String getTotalPages() {

		return this.toString(totalPages);

	}

	public String getTotalRow() {

		return this.toString(totalRows);

	}

	public int getPageRecorders() {

		return pageRecorders;

	}

	public int getPageEndRow() {

		return pageEndRow;

	}

	public int getPageStartRow() {

		return pageStartRow;

	}

	public boolean isHasNextPage() {

		return hasNextPage;

	}

	public boolean isHasPreviousPage() {

		return hasPreviousPage;

	}

	public List getNextPage() {

		currentPage = currentPage + 1;

		if ((currentPage - 1) > 0) {

			hasPreviousPage = true;

		}
		else {

			hasPreviousPage = false;

		}
		if (currentPage >= totalPages) {

			hasNextPage = false;

		}
		else {

			hasNextPage = true;

		}
		List list = getBooks();

		return list;

	}

	public List getPreviousPage() {

		currentPage = currentPage - 1;

		if (currentPage == 0) {

			currentPage = 1;

		}
		if (currentPage >= totalPages) {

			hasNextPage = false;

		}
		else {

			hasNextPage = true;

		}
		if ((currentPage - 1) > 0) {

			hasPreviousPage = true;

		}
		else {

			hasPreviousPage = false;

		}
		List list = getBooks();

		return list;

	}

	public List getIndexPage(String leafNumber) {

		currentPage = Integer.valueOf(leafNumber.trim());

		if (currentPage == 0) {
			currentPage = 1;

		}

		if (currentPage >= totalPages) {
			hasNextPage = false;

		}

		else {
			hasNextPage = true;
		}

		if ((currentPage - 1) > 0) {
			hasPreviousPage = true;

		}

		else {
			hasPreviousPage = false;

		}

		List list = getBooks();

		return list;

	}
	/**
	 * .----------------------------------------------------------------------------------------------------------- * * *
	 * -----------------------------------------------------------------------------------------------------------
	 */
	public List getBooks() {
		if (currentPage * pageRecorders < totalRows) {

			pageEndRow = currentPage * pageRecorders;

			pageStartRow = pageEndRow - pageRecorders;

		}

		else {

			pageEndRow = totalRows;

			pageStartRow = pageRecorders * (totalPages - 1);

		}
		List list = new ArrayList();
		
		list = queryHqlList(SearcHql, pageStartRow, pageRecorders);

		return list;
	}

	public String toString(int temp) {

		String str = Integer.toString(temp);

		return str;

	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	public void setPageEndRow(int pageEndRow) {
		this.pageEndRow = pageEndRow;
	}

	public void setPageStartRow(int pageStartRow) {
		this.pageStartRow = pageStartRow;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
}
